from appium import webdriver

import time

from selenium.webdriver.common.by import By

desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '111'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 1>通过 id 的形式，获取所有 resource-id 为 ”com.android.settings:id/title“ 的元素，并打印其文字内容
titles = driver.find_elements(By.ID, 'com.android.settings:id/title')

# 打印所有的文本元素
# for title in titles:
#     print(title.text)

# 对第三个元素进行点击
# titles[3].click()


# 2>通过 class_name 的形式，获取所有class 为 ”android.widget.TextView“ 的元素，并打印其文字内容
views = driver.find_elements(By.CLASS_NAME, 'android.widget.TextView')
for view in views:
    print(view.text)

# 3>通过 xpath 的形式，获取所有包含 ”设“ 的元素，并打印其文字内容
# eles = driver.find_elements(By.XPATH, "//*[contains(@text,'设')]")
# for i in eles:
#     print(i.text)

time.sleep(5)
driver.quit()
